package server

import (
	"context"

	"qq_service/end_email/protobuf/pb"
	//"strconv"
	"github.com/go-gomail/gomail"
)

const Address = "127.0.0.1:5000"

//服务结构体
type SendEmail struct {
	pb.UnimplementedSendMailServiceServer
}

//HelloRequest请求
//HelloResponse返回结果
func (h SendEmail) SendMail(ctx context.Context, in *pb.QqListRequest) (*pb.QqListResponse, error) {
	resp := new(pb.QqListResponse)

	var isok = SendResult(in.Email, in.Word, in.QqPath)
	if isok {
		resp.IsOk = "ok"
	} else {
		resp.IsOk = "no"
	}

	return resp, nil
}
func SendResult(email string, word string, filepathlist []string) bool {
	m := gomail.NewMessage()
	m.SetHeader("From", "yinc13@mails.tsinghua.edu.cn")
	m.SetHeader("To", email, "yinc13@mails.tsinghua.edu.cn")
	m.SetAddressHeader("Cc", "yinc13@mails.tsinghua.edu.cn", "专业营销数据服务")
	m.SetHeader("Subject", "尊敬的客户你要的数据到了-关于"+word)
	m.SetBody("text/html", "尊敬的用户您好，营销数据请查收")
	for i := 0; i < len(filepathlist); i++ {
		m.Attach(filepathlist[i]) //追加附件
	}
	d := gomail.NewDialer("smtp.tsinghua.edu.cn", 25,
		"yinc13@mails.tsinghua.edu.cn", "tsinghua8848OK")
	if err := d.DialAndSend(m); err != nil {
		return false
	} else {
		return true
	}

}
